from django.contrib.auth.models import AbstractUser
from django.db import models

class User(AbstractUser):
    ROLE_CHOICES = (
        ('admin', '系统管理员'),
        ('purchaser', '采购员'),
        ('salesman', '销售员'),
        ('delivery_admin', '配送管理员'),
        ('deliveryman', '配送员'),
        ('customer', '客户'),
        ('supplier', '供应商'),
    )
    
    role = models.CharField(max_length=20, choices=ROLE_CHOICES, verbose_name='角色')
    phone = models.CharField(max_length=11, verbose_name='电话号码', blank=True, null=True, default='')
    address = models.TextField(verbose_name='地址', blank=True, default='')
    company_name = models.CharField(max_length=100, verbose_name='公司名称', blank=True, default='')
    business_license = models.CharField(max_length=100, verbose_name='营业执照号', blank=True, default='')
    
    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    def has_purchase_permission(self):
        return self.role in ['admin', 'purchaser']

    def has_sales_permission(self):
        return self.role in ['admin', 'salesman']

    def has_delivery_permission(self):
        return self.role in ['admin', 'deliveryman', 'delivery_admin']

    def has_supplier_permission(self):
        return self.role == 'supplier'

    def has_customer_permission(self):
        return self.role == 'customer'

class Supplier(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='supplier_profile')
    company_name = models.CharField(max_length=100, verbose_name='公司名称')
    business_license = models.CharField(max_length=100, verbose_name='营业执照号')
    
    class Meta:
        verbose_name = '供应商'
        verbose_name_plural = verbose_name

class DeliveryStaff(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='delivery_profile')
    vehicle_number = models.CharField(max_length=20, verbose_name='车牌号')
    service_area = models.CharField(max_length=100, verbose_name='服务区域')
    
    class Meta:
        verbose_name = '配送人员'
        verbose_name_plural = verbose_name
